// pages/dianzan/dianzan.js
const envId = "cloud-2gdkk3oy7a554788"
const db = wx.cloud.database()
const cmd = db.command
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },
    onShareAppMessage: function () {
        return {
            title: '分享六爻策给你的伙伴吧',
            path: '/pages/index/index',
            imageUrl: '/images/6YCSHARE.png'
        }
    },
    autoStyle: function () {
        var systeminfo = wx.getSystemInfoSync();
        console.log(systeminfo);

        //px为单位
        var sW = systeminfo.screenWidth;
        var sH = systeminfo.screenHeight

        var rate = 750 / sW;
        console.log(rate);

        //rpx为单位
        var rW = parseInt(sW * rate)
        var rH = parseInt(sH * rate)

        //屏幕长宽比
        var hwNum = sH / sW

        //pad的参数
        var pam = 1
        if (hwNum < 1.4) {
            pam = 0.72
        }

        //设置按钮的高度和大小
        var bH = parseInt(rH * 0.05 * pam)
        var bW = parseInt(rW * 0.81)
        var bSize = parseInt(rH * 0.025 * pam)

        this.setData({
            bH: bH,
            bW: bW,
            bSize: bSize,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.autoStyle()

        //获取用户的openId
        var openId = wx.getStorageSync("openId")
        if (!openId) {
            //不存在再获取openId
            this.getOpenId()
        } else {
            console.log(this.data);
            console.log(openId);
            console.log("openId已存在，无须获取");
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    getOpenId: function () {
        wx.cloud.callFunction({
            name: 'main',
            config: {
                env: envId
            },
            data: {
                type: 'getOpenId'
            }
        }).then((resp) => {
            console.log("OPEN_ID : " + resp.result.openid);
            wx.setStorageSync("openId", resp.result.openid)

        }).catch((e) => {
            console.log(e);
        });
    },
    submit: function () {
        var comment = this.data.comment

        if (comment) {
            var date = new Date()
            wx.showLoading({
                title: '保存中',
            })
            db.collection('LY_COMMENT').add({
                // data 字段表示需新增的 JSON 数据
                data: {
                    date: date,
                    comment: comment
                }
            }).then(res => {
                console.log(res)
                //清空输入框
                this.setData({
                    comment: ''
                })
                wx.showToast({
                    title: '已收藏您的建议，谢谢点评',
                    icon: 'none',
                    duration: 1500
                })
            }).catch(console.error)
            wx.hideLoading();
        } else {
            wx.showToast({
                title: '请您评论后再保存哦！',
                icon: 'none',
                duration: 1500
            })
        }
    },
    comment: function (e) {
        this.setData({
            comment: e.detail.value
        })
    },
})